<!DOCTYPE html>
<html>
<head>
	<title>购物车</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no, maximum-scale=1.0"/>
	<meta content="yes" name="apple-mobile-web-app-capable" />
	<meta content="black" name="apple-mobile-web-app-status-bar-style" />
	<meta content="telephone=no" name="format-detection" />
	<link href="{G_TEMPLATES_CSS}/wx/cart.css" rel="stylesheet">
	<link href="{G_TEMPLATES_CSS}/wx/comm.css" rel="stylesheet">
	<script src="{G_TEMPLATES_JS}/wx/jquery-2.1.4.min.js"></script>
	<script type="text/javascript" src='{G_TEMPLATES_JS}/wx/common.js'></script>
	<style>
		body{
			background-color:#F3F3F3;
		}
	</style>
</head>
<body>
	<header class="header">
		<span class="name">购物车</span>
		<img src="{G_TEMPLATES_IMAGE}/wx/bianji.png" class="edit e-click">
		<span class="remove e-click" style="display:none">取消</span>
	</header>

	<div class="main">
	{wc:if $shop!=0}
		<ul>
		{wc:php:start}
		$buyshopmoney=0;
		{wc:php:end}
        {wc:loop $shoplist  $key $val}
		{wc:php:start}
    	   $num = count($shoplist);
		   $buyshopmoney+=$Mcartlist[$val['id']]['money'];
        {wc:php:end}  
			<li>
				<img src="{G_TEMPLATES_IMAGE}/wx/shanchu_weixuan.png" alt="check" class="check e-click" state="0">
				<img src="{G_UPLOAD_PATH}/{wc:$val['thumb']}" class="goods-img">
				<span class="goods-name e-click"><a href="{WEB_PATH}/mobile/mobile/item/{wc:$val['id']}" class="gray6" style="font-size:16px;">(第{wc:$val['qishu']}期){wc:$val['title']}</a></span>
				<span class="goods-sy" style="font-size:14px;">剩余人次:<span class="sy-num">{wc:$val['zongrenshu']-$val['canyurenshu']}</span></span>
				<span class="num e-click" data-goodid="{wc:$val['id']}" data-yunjiage = "{wc:$val['yunjiage']}">
					<a href="javascript:void(0);" class="jian {wc:if $Mcartlist[$val['id']]['num']==1}z-jiandis{wc:if:end}">—</a>
					<input type="number" class="goods-num"  value="{wc:$Mcartlist[$val['id']]['num']}">
					<a href="javascript:void(0);" class="jia {wc:if $Mcartlist[$val['id']]['num']==$val['zongrenshu']}z-jiadis{wc:if:end}">+</a>
					<div class="num-bw">包尾</div>
				</span>
				<img src="{G_TEMPLATES_IMAGE}/wx/shanchu.png" class="delete-img e-click" data-goodid="{wc:$val['id']}" >
			</li>
            
        {wc:loop:end}
            
		</ul>
	</div>
  <!-----购物车开始---->
  <div class="shopping">
  	<ul>
    	<a href="#">
        	<li style="width:30%">
            	<span class="sum e-click">总计&nbsp;:&nbsp;<span style="color:#f75555;" id="totalmoney">{wc:$buyshopmoney}.00元</span></span>
                <span class="remove e-click" style="display:none">全选</span>
            </li>
        </a>
        
        <a href="#">
        	<li style="width:40%">
            	<span class="sum e-click">共计{wc:$num}件</span>
                <!---<span class="remove e-click" style="display:none;">已选6件</span>-->
            </li>
        </a>
        
        <a href="#">
        	<li style="width:29%;border-left:1px #f3f3f3 solid;">
            	<span class="submit e-click">提交订单</span>
                <!--<span class="remove e-click" style="display:none;">删除订单</span>-->
            </li>
        </a>
        <div style="clear:both;"></div>
    </ul>
  </div>
  <!-----购物车结束---->
  {wc:if:end}
{wc:templates "mobile/index","footer"}
</body> 
<script>
 var Gobal = new Object();
  Gobal.Skin="{G_TEMPLATES_STYLE}";  
  Gobal.Webpath = "{WEB_PATH}";
	$(document).ready(function(e) {
		var totle=$(".main ul li").length;
		$(".goods-totle").text(totle);
		$(".edit").click(function(){
			$(".edit").attr("style","display:none;");
			$(".remove").attr("style","");
			$(".check").attr("style","display:block");
			$(".num").attr("style","margin-left:20px");
			$(".goods-name").attr("style","width:52%");
			$(".sub-dd").attr("style","display:none;");
			$(".delete").attr("style","display:block");
			$(".delete-img").attr("style","display:none")
		})
		
		$(".remove").click(function(){
			$(".e-click").attr("style","");
			$(".remove").attr("style","display:none");
			$(".delete").attr("style","display:none;");
		})

		$(".check").click(function(){
			var sw=$(".check").index(this);
			var state=$(".check").eq(sw).attr("state");
			if(state==0){
				$(".check").eq(sw).attr("src","{G_TEMPLATES_IMAGE}/wx/shanchu_xuanzhong.png");
				$(".check").eq(sw).attr("state","1");
				checkNum();
			}else{
				$(".check").eq(sw).attr("src","{G_TEMPLATES_IMAGE}/wx/shanchu_weixuan.png");
				$(".check").eq(sw).attr("state","0");
				checkNum();
			}
		})
		$(".all-check").click(function(){
			var state=$(".all-check").attr("state");
			if(state==0){
				$(".all-check").attr("src","{G_TEMPLATES_IMAGE}/wx/shanchu_xuanzhong.png");
				$(".check").attr("src","{G_TEMPLATES_IMAGE}/wx/shanchu_xuanzhong.png");
				$(".check").attr("state","1");
				$(".all-check").attr("state","1");
				checkNum();
			}else{
				$(".all-check").attr("src","{G_TEMPLATES_IMAGE}/wx/shanchu_weixuan.png");
				$(".check").attr("src","{G_TEMPLATES_IMAGE}/wx/shanchu_weixuan.png");
				$(".check").attr("state","0");
				$(".all-check").attr("state","0");
				checkNum();
			}
		})
		$(".goods-num").blur(function(){
			var sw=$(".goods-num").index(this);
			var num=$(".goods-num").eq(sw).val();
			var myreg = /^\+?[0-9][0-9]*$/;
			var maxnum=$(".sy-num").eq(sw).text();
			if(!myreg.test(num)){
				alert("请输入大于0的正整数！");
				$(".goods-num").eq(sw).val(0);
				nTotle();
			}else{
				if(parseInt(num)>parseInt(maxnum)){
					alert("输入人次超出剩余人次！");
					$(".goods-num").eq(sw).val(maxnum);
					$(".jia").eq(sw).attr("style","border:1px solid #ccc;color:#ccc");
					$(".jian").eq(sw).attr("style","border:1px solid #646464;color:#646464");
					nTotle();
				}else{
					$(".jian").eq(sw).attr("style","border:1px solid #646464;color:#646464");
					nTotle();
				}
			}
		})
		
		$(".jian").click(function(){
			var sw=$(".jian").index(this);
			$(".jia").eq(sw).attr("style","border:1px solid #646464;color:#646464");
			var num=$(".goods-num").eq(sw).val();
			var t=$(this).parent().data('goodid');
			var p=$(this).parent().data('yunjiage');
			if(num>=1){
				num--;
				$(".goods-num").eq(sw).val(num);
				nTotle();
				GetJPData(Gobal.Webpath, "ajax", "addShopCart/" + t + "/" + num+"/cart", 
	         
           function(w) {
                if (w.code == 1) {
                   alert('本期产品卖光了');
                } else {
                    if (w.code == 0) {
                    	var d=$("#totalmoney").text();
                    	 p=parseInt(d)-parseInt(p);
                    	$("#totalmoney").html(+p+'.00元');
                    }
                }
            })

			}


			if(num==0){
				$(".jian").eq(sw).attr("style","border:1px solid #ccc;color:#ccc");
				$(".goods-num").eq(sw).val(num);
			}	
		})
		
		$(".jia").click(function(){
			var sw=$(".jia").index(this);
			$(".jian").eq(sw).attr("style","border:1px solid #646464;color:#646464");
			var num=$(".goods-num").eq(sw).val();
			var maxnum=$(".sy-num").eq(sw).text();
			if(parseInt(num)<parseInt(maxnum)){
				num++;
				$(".goods-num").eq(sw).val(num);
				nTotle();
			}
			var t=$(this).parent().data('goodid');
			var p=$(this).parent().data('yunjiage');

		GetJPData(Gobal.Webpath, "ajax", "addShopCart/" + t + "/" + num+"/cart", 
	         
           function(w) {
                if (w.code == 1) {
                   alert('本期产品卖光了');
                } else {
                    if (w.code == 0) {
                    	var d=$("#totalmoney").text();
                    	 p=parseInt(d)+parseInt(p);
                    	$("#totalmoney").html(+p+'.00元');
                    }
                }
            })
			if(parseInt(num)==parseInt(maxnum)){
				$(".jia").eq(sw).attr("style","border:1px solid #ccc;color:#ccc");
				$(".goods-num").eq(sw).val(num);
			}
		})
		$(".delete-img").click(function(){
			var sw=$(this).data('goodid');
			del(sw);
		})
		$(".delete-all").click(function(){
			del();
		})
		$(".num-bw").click(function(){
			var sw=$(".num-bw").index(this);
			var maxnum=$(".sy-num").eq(sw).text();
			$(".goods-num").eq(sw).val(maxnum);
			$(".jia").eq(sw).attr("style","border:1px solid #ccc;color:#ccc");
			$(".jian").eq(sw).attr("style","border:1px solid #646464;color:#646464");
			nTotle();
		})
		function nTotle(){
			var len=$(".main ul li").length;
			var pr=0;
			for(var i=0;i<len;i++){
				var num=$(".goods-num").eq(i).val();
				pr=parseInt(pr)+parseInt(num);
			}
			$(".goods-hj").text(pr);
		}
		function del(sw){
			if(confirm("确认删除吗")){
				$.getJSON('{WEB_PATH}/index.php/mobile/ajax/delCartItem/'+sw, function(data){
					if (data.code==0) {
						window.location.reload();
					}
				});
			}
			else{
				alert("no")
				return;
			}
		}
		function checkNum(){
			var num=0;
			var len=$(".check").length;
			for(var i=0;i<len;i++){
				var state=$(".check").eq(i).attr("state");
				if(state==1){
					num++;
				}
			}
			$(".goods-check").text(num);
		}
	});
</script>
</html>
